/*
 * @Description:
 * @Version: 1.0.0
 * @Autor: zhou
 * @Date: 2022-03-19 17:20:36
 * @LastEditors: zhou
 * @LastEditTime: 2022-03-24 15:10:44
 */
import io from "socket.io-client";

const mySocket = {
    start: false,
    stop: false,
    socket: null,
    serverClose: false,
    initSocket(callback) {
        // 用户登录重新连接
        const options = { path: "/oa/" };
        if (!this.start || !this.socket) {
            this.start = true;
            this.socket = io(process.env.VUE_APP_SOCKET_URL, options);
            this.socket.on("connect", (evt) => {
                if (callback && typeof callback === "function") {
                    callback();
                }
                console.log("初始化连接socket");
            });
        } else if (this.stop) {
            this.stop = false;
            this.socket.on("reconnect", (evt) => {
                console.log("重连连接socket = ", evt);
                if (callback && typeof callback === "function") {
                    callback(evt);
                }
            });
        }
    },
    emit(eventName, data) {
        this.socket.emit(eventName, data);
    },
    on(eventName, callback) {
        this.socket.on(eventName, (evt) => {
            if (callback && typeof callback === "function") {
                callback(evt);
            }
        });
    },
    stopSocket() {
        this.socket.disconnect();
    },
};

export default mySocket;
